<template>
	<view class="container">
		<view class="arrow" :style="{marginTop:statusBarHeight+'px'}">
			<uni-icons type="arrow-left" size="30" @click="back"></uni-icons>
		</view>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="loginPop.msgType" :message="loginPop.messageText"></uni-popup-message>
		</uni-popup>
		<h2 class="login">登录！</h2>
		<p class="login-desc">
			欢迎回来！
		</p>
		<div class="form">
			<uni-forms :modelValue="formData">
				<div class="label">
					<input type="text" v-model="formData.username" placeholder="请输入账号" class="_ipt" />
				</div>
				<div class="label">
					<input type="password" v-model="formData.password" placeholder="请输入密码" class="_ipt" />
				</div>
				<button class="button" @click="doLoginsubmit" :loading="loginBtn" :disabled="loginBtn">登录</button>
			</uni-forms>
		</div>
		<view class="footer">
			点击登录，即同意<view @click="openPrivacy" class="text">隐私政策</view>和<view @click="openPrivacy" class="text">服务条款
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		doLogin
	} from '@/components/kevinrong-http/user.js'
	export default {
		data() {
			return {
				formData: {
					username: '',
					password: ''
				},
				statusBarHeight: 0,
				loginBtn: false,
				loginPop: {
					type: 'center',
					msgType: 'success',
					messageText: '',
				}
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),
		},
		mounted() {
			this.GetStatusBarHeight()
		},
		methods: {
			...mapMutations(['userLogin']),
			openPrivacy() {
				// #ifdef MP-WEIXIN
				wx.openPrivacyContract()
				// #endif

				// #ifndef MP-WEIXIN
				//其他平台进入隐私条款
				uni.navigateTo({
					url: 'pages/user/privacy'
				})
				// #endif
			},
			GetStatusBarHeight() {
				// #ifdef MP-WEIXIN
				const SystemInfo = wx.getSystemInfoSync();
				this.statusBarHeight = SystemInfo.statusBarHeight / 2;
				// #endif
			},
			doLoginsubmit() {

				return false
				if (!this.formData.username) {
					this.popmessage('用户名不能为空')
					return false;
				} else if (!this.formData.password) {
					this.popmessage('密码不能为空')
					return false;
				}
				this.loginBtn = true
				doLogin(this.formData).then(r => {
					this.loginBtn = false
					if (r.data.code != 200) {
						this.popmessage(r.data.message)
						return false;
					} else {
						this.userLogin(r.data.data)
						this.popmessage('登录成功', 'sccuess')
						this.back()
						return false;
					}
				}, data => {
					this.popmessage('Error')
					this.loginBtn = false
				})
			},
			popmessage(m, type) {
				this.loginPop.msgType = type || 'error'
				this.loginPop.messageText = m
				this.$refs.message.open()
			},
			back() {
				let routes = getCurrentPages()
				if (routes.length >= 2) {
					uni.navigateBack({
						delta: 2
					})
				}else{
					uni.switchTab({
						url:'/pages/user/my'
					})
				}
			}
		}
	}
</script>
<style lang="scss" scoped>
	.login {
		font-size: 44rpx;
		font-weight: bold;
		margin-top: 100rpx;

	}

	.container {
		padding: 120rpx 60rpx;
		color: $h2FontColor;
		background-color: #fafafa;
		min-height: calc(100vh - 240rpx);
		position: relative;

		.footer {
			position: absolute;
			bottom: 40rpx;
			width: calc(100% - 120rpx);
			color: $descColof;
			display: flex;
			justify-content: center;
			font-size: 26rpx;

			.text {
				color: $baseBtnColor;
				font-weight: bold;
				margin: 0 5rpx;
			}
		}

		.form {
			margin-top: 100rpx;

			.button {
				margin-top: 90rpx;
				height: 110rpx;
				line-height: 110rpx;
				background-color: $baseBtnColor;
				color: #fff;

				&[disabled] {
					background-color: #eee;
					color: #999;
				}

				&[loading]:before {
					margin-right: 10rpx;
				}
			}

			.label {
				margin-bottom: 30rpx;
			}

			._ipt {
				background-color: #fff;
				border-radius: 10rpx;
				height: 110rpx;
				font-size: 36rpx;
				line-height: 110rpx;
				padding: 0 30rpx;

				.input-placeholder {
					color: $desc2Color;
				}
			}
		}

		.login-desc {
			color: $descColof;
			font-size: 32rpx;
			margin: 5rpx 0;
		}
	}
</style>